package com.myaihui.controller;

import com.myaihui.config.FileConfig;
import com.myaihui.domain.Attachment;
import com.myaihui.domain.Case;
import com.myaihui.domain.Content;
import com.myaihui.domain.SysUser;
import com.myaihui.foundation.model.Pager;
import com.myaihui.foundation.msg.MsgDefinition;
import com.myaihui.foundation.msg.ResultMsg;
import com.myaihui.foundation.query.ReportQuery;
import com.myaihui.service.FileService;
import com.myaihui.service.ReportService;
import com.myaihui.util.LoginUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by sun on 2018/6/13.
 *
 * @author sunfuchang03@126.com
 * @version 1.0
 * @since 1.0
 */
@Controller
@RequestMapping("/pm/report")
public class PMReportController {
    /*
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     *
     * Static fields/constants/initializer
     *
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     */

    private static Logger log = LoggerFactory.getLogger(PMReportController.class);

    /*
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     *
     * Instance fields
     *
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     */

    @Autowired
    private ReportService reportService;
    @Autowired
    private FileConfig fileConfig;
    @Autowired
    private FileService fileService;

    /*
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     *
     * Constructors
     *
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     */



    /*
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     *
     * Public Methods
     *
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     */

    @GetMapping({"/", ""})
    @RequiresPermissions("report:page:home")
    public String home(Model model) {
        return "report/home";
    }

    @PostMapping("/page")
    @ResponseBody
    @RequiresPermissions("report:page:home")
    public ResultMsg queryPageData(ReportQuery query) {
        SysUser sysUser = LoginUtil.loginUser();
        query.setPlatformId(sysUser.getPlatformId());
        query.setPlatformType(Case.PLATFORM_TYPE_PM);
        Pager<Case> contentPager = reportService.pageQuery(query);
        return ResultMsg.success(contentPager);
    }

    @GetMapping("/{id}")
    public String detail(@PathVariable String id, Model model) {
        Case cs = reportService.get(id);
        String imageIds = cs.getImageIds();
        if (StringUtils.hasLength(imageIds)) {
            String[] ids = imageIds.split(",");
            List<String> urls = new ArrayList<>();
            for (String attaId : ids) {
                Attachment attachment = fileService.get(attaId);
                urls.add(fileConfig.getUrl() + attachment.getPath());
            }
            model.addAttribute("imageUrls", urls);
        }
        model.addAttribute("cs", cs);
        return "report/detail";
    }

    @PostMapping("/process/{id}")
    @ResponseBody
    public ResultMsg saveOrUpdate(@PathVariable String id, String feedback) {
        Case cs = reportService.get(id);
        cs.setState(Case.State.PROCESSED);
        cs.setFeedback(feedback);
        cs.setUpdateTime(new Date());
        SysUser sysUser = LoginUtil.loginUser();
        cs.setUpdateUserId(sysUser.getId());

        reportService.update(cs);
        return ResultMsg.success(cs);
    }

    @PostMapping("/delete/{id}")
    @ResponseBody
    public ResultMsg delete(@PathVariable String id) {
        if (!StringUtils.hasLength(id)) {
            return ResultMsg.error(MsgDefinition.EMPTY_ARGUMENTS);
        }
        reportService.delete(id);
        return ResultMsg.success(true);
    }

    /*
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     *
     * Private Methods
     *
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     */


}